package com.shp.controller;

import com.shp.pojo.*;
import com.shp.service.GoodsService;
import com.shp.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ZWL
 * @Version: 1.0
 * @create: 2020/2/26 14:07
 */
@Controller
@RequestMapping(value = "/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private GoodsService goodsService;


    /**
     * 立即购买
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultInfo addOrders(HttpServletRequest request,@RequestBody Orders orders) {
        ResultInfo info = new ResultInfo();
        User activeUser = (User)request.getSession().getAttribute("activeUser");
        Integer userId = activeUser.getId();
        // 判断是否是自己的商品
        if(goodsService.getUserByUserIdAndGoodsId(orders.getGoods_id(),userId) != null){
            info.setErrorMsg("不可以购买自己的商品");
            return info;
        }
        return ordersService.buyNow(request,orders,info);
    }

    /**
     * 用户在购物车结算时，将结算得到的数据保存到session中
     * @param goodsList
     * @param session
     * @return
     */
    @RequestMapping(value = "/msg",method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo confirmOrder(@RequestBody List<Good> goodsList, HttpSession session){
        ResultInfo info = new ResultInfo();
        if(goodsList != null && goodsList.size() > 0){
            List<Orders> ordersList = ordersService.getOrdersByGoods(goodsList);
            float singlePrice = 0;
            float totalPrice = 0;
            int num = 0;
            // 设置需求数
            for(Orders orders : ordersList){
                singlePrice = orders.getGoodsExtend().getGoods().getPrice();
                for(Good goods : goodsList){
                    if (orders.getGoods_id() == (int)goods.getId()){
                        num = goods.getNumber();
                        orders.setNeed_num(num);
                        orders.setOrder_price(num * singlePrice);
                    }
                }
                totalPrice = totalPrice + num * singlePrice;
            }
            session.setAttribute("totalPrice",totalPrice);
            session.setAttribute("ordersList",ordersList);
            info.setFlag(true);
        }else{
            info.setErrorMsg("结算失败");
        }
        return info;
    }

    /**
     * 将session中用户的订单信息，传递到订单页面
     * @return
     */
    @RequestMapping(value = "/myorders",method = RequestMethod.GET)
    public String myOrders(){
        return "/goods/orders_msg";
    }

    /**
     * 确认购买,将订单信息保存到数据库
     * @return
     */
    @RequestMapping(value = "/confirm",method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo confirmBuy(@RequestBody OrderBatch orderBatch, HttpSession session){
        ResultInfo info = new ResultInfo();
        // 获取session中的订单信息
        List<Orders> ordersList = (List<Orders>)session.getAttribute("ordersList");
        User activeUser = (User)session.getAttribute("activeUser");
        info = ordersService.saveOrdersMul(ordersList, activeUser, orderBatch,info);
        return info;
    }

    /**
     * 重定向到购物车
     * @return
     */
    @RequestMapping(value = "/cancel")
    public String cancelOrders(HttpSession session){
        session.removeAttribute("totalPrice");
        session.removeAttribute("ordersList");
        return "redirect:/shopcart/mycart";
    }


}
